package com.yanqu.road.server.cmd.activity.xingyoushanshui;

import com.yanqu.road.entity.activity.youxing.config.XingYouConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.XingYouShanShuiProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.shanshui.XingYouShanShuiMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;

@CmdApi(reqMsg = XingYouShanShuiProto.ShanShuiRecoverEnergyReq.class, respMsg = XingYouShanShuiProto.ShanShuiRecoverEnergyRsp.class)
@Cmd(code = Protocol.S_XING_YOU_SHAN_SHUI_RECOVER_ENERGY, desc = "恢复体力")
public class ShanShuiRecoverEnergyCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        XingYouConfig config = XingYouShanShuiMgr.getConfig();
        if (config == null || !ActivityMgr.activityInTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }

        XingYouShanShuiProto.ShanShuiRecoverEnergyReq  reqMsg = XingYouShanShuiProto.ShanShuiRecoverEnergyReq.parseFrom(packet.getBytes());
        int num = reqMsg.getUserNum();
        if(num < 0){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        Property property = new Property();
        property.addProperty(config.getSHANSHUI_TIPS_ID(), num);
        if(!player.getModule(CurrencyModule.class).currencyIsEnough(property)){
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }
        // 预扣除
        player.getModule(PreReduceModule.class).preReduce(property);

        player.sendUnionActivityGroupPacket(Protocol.C_XING_YOU_SHAN_SHUI_RECOVER_ENERGY, reqMsg.toBuilder(), config.getActivityInfo().getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_XING_YOU_SHAN_SHUI_RECOVER_ENERGY, XingYouShanShuiProto.ShanShuiRecoverEnergyRsp.newBuilder().setRet(ret));
        }
    }
}
